package com.changgou.goods.service.impl;

import com.alibaba.fastjson.JSON;
import com.changgou.common.pojo.IdWorker;
import com.changgou.core.service.impl.CoreServiceImpl;
import com.changgou.goods.dao.BrandMapper;
import com.changgou.goods.dao.CategoryMapper;
import com.changgou.goods.dao.SkuMapper;
import com.changgou.goods.dao.SpuMapper;
import com.changgou.goods.pojo.*;
import com.changgou.goods.service.SpuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

/****
 * @Author:admin
 * @Description:Spu业务层接口实现类
 * @Date 2019/6/14 0:16
 *****/
@Service
public class SpuServiceImpl extends CoreServiceImpl<Spu> implements SpuService {

    private SpuMapper spuMapper;
    @Autowired
    private SkuMapper skuMapper;

    @Autowired
    public SpuServiceImpl(SpuMapper spuMapper) {
        super(spuMapper, Spu.class);
        this.spuMapper = spuMapper;
    }
    @Autowired
    private IdWorker idWorker;

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private BrandMapper brandMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveGoods(Goods goods) {
        Spu spu = goods.getSpu();
        if (spu.getId()!=null){
            spuMapper.updateByPrimaryKey(spu);
            Sku sku =new Sku();
            sku.setSpuId(spu.getId());
            skuMapper.delete(sku);
        }else{
            long spuId = idWorker.nextId();
            spu.setId(""+spuId);
            spuMapper.insertSelective(spu);
        }
        addList(goods, spu);
    }

    private void addList(Goods goods, Spu spu) {
        List<Sku> skuList = goods.getSkuList();
        for (Sku sku : skuList) {
            long skuId = idWorker.nextId();
            sku.setId(""+skuId);
            String name = spu.getName();
            Map<String,String> map = JSON.parseObject(sku.getSpec(), Map.class);
            for (Map.Entry<String, String> stringStringEntry : map.entrySet()) {
                String value = stringStringEntry.getValue();
                name+=" "+value;
            }
            sku.setName(name);
            sku.setCreateTime(new Date());
            sku.setUpdateTime(sku.getCreateTime());
            sku.setSpuId(""+spu.getId());
            Category category = categoryMapper.selectByPrimaryKey(spu.getCategory3Id());
            if(category != null){
                sku.setCategoryName(category.getName());
                sku.setCategoryId(spu.getCategory3Id());
            }
            Brand brand = brandMapper.selectByPrimaryKey(spu.getBrandId());
            if(brand != null){
                sku.setBrandName(brand.getName());
            }
            sku.setStatus("1");
            skuMapper.insertSelective(sku);
        }
    }

    @Override
    public Goods findGoodsById(String id) {
        Spu spu = spuMapper.selectByPrimaryKey(id);
        Sku t = new Sku();
        t.setSpuId(id);
        List<Sku> skuList = skuMapper.select(t);
        Goods goods =new Goods();
        goods.setSpu(spu);
        goods.setSkuList(skuList);
        return goods;
    }
}
